package com.example.itemwriter.tomulti;

import com.example.pojo.Customer;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.batch.core.configuration.annotation.StepScope;
import org.springframework.batch.item.ItemWriter;
import org.springframework.batch.item.file.FlatFileItemWriter;
import org.springframework.batch.item.file.transform.LineAggregator;
import org.springframework.batch.item.support.ClassifierCompositeItemWriter;
import org.springframework.batch.item.support.CompositeItemWriter;
import org.springframework.batch.item.xml.StaxEventItemWriter;
import org.springframework.classify.Classifier;
import org.springframework.context.annotation.Bean;
import org.springframework.core.io.FileSystemResource;
import org.springframework.oxm.xstream.XStreamMarshaller;
import org.springframework.stereotype.Component;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

/**
 * @author XieYa
 */
@Component
public class MultiFileWriterConfig {

    /**
     * fileWriter 输出数据到txt文件
     */
    @Bean
    public FlatFileItemWriter<Customer> jsonFileWriter() {
        //把Customer对象转成字符串输出到文件
        FlatFileItemWriter<Customer> writer = new FlatFileItemWriter<>();
        String path = "D:\\customer.txt";
        writer.setResource(new FileSystemResource(path));
        //把Customer对象转成字符串
        writer.setLineAggregator(new LineAggregator<Customer>() {
            final ObjectMapper mapper = new ObjectMapper();
            @Override
            public String aggregate(Customer customer) {
                String str = null;
                try {
                    str = mapper.writeValueAsString(customer);
                } catch (JsonProcessingException e) {
                    e.printStackTrace();
                }
                return str;
            }
        });
        try {
            writer.afterPropertiesSet();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return writer;

    }

    /**
     * xmlWriter 输出数据到xml文件
     */
    @Bean
    public StaxEventItemWriter<Customer> xmlWriter() {
        StaxEventItemWriter<Customer> writer = new StaxEventItemWriter<>();
        XStreamMarshaller marshaller = new XStreamMarshaller();
        //告诉marshaller把数据转成什么类型
        Map<String, Object> map = new HashMap<>(1);
        map.put("customer", Customer.class);
        marshaller.setAliases(map);
        writer.setRootTagName("customers");
        writer.setMarshaller(marshaller);
        String path = "D:\\cus.xml";
        writer.setResource(new FileSystemResource(path));
        try {
            writer.afterPropertiesSet();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return writer;
    }


    /**
     * multiFileItemWriter 调用输出到单个文件操作来实现输出数据到多个文件
     */
//    @Bean
//    public CompositeItemWriter<Customer> multiFileItemWriter() {
//        CompositeItemWriter<Customer> writer = new CompositeItemWriter<>();
//        //输出到两个不同的文件中
//        writer.setDelegates(Arrays.asList(jsonFileWriter(), xmlWriter()));
//        try {
//            writer.afterPropertiesSet();
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//        return writer;
//    }


    /**
     * multiFileItemWriter  按照某种条件对数据进行分类存储不同文件
     */
    @Bean
    public ClassifierCompositeItemWriter<Customer> multiFileItemWriter() {
        ClassifierCompositeItemWriter<Customer> writer = new ClassifierCompositeItemWriter<>();
        //分类，比如按照年龄分成两个文件
        writer.setClassifier((Classifier<Customer, ItemWriter<? super Customer>>) customer -> {
            //按照Customer的id进行分类
            return customer.getId() % 2 == 0 ? jsonFileWriter() : xmlWriter();
        });
        return writer;
    }
}
